import java.awt.Point;
import java.util.Vector;

import junit.framework.TestCase;


public class MassTest extends TestCase {
	public void testDetectBoundaryOneMass() throws Exception {
		Vector<Point> controlPoints = new Vector<Point>();
		controlPoints.add(new Point(4, 4));

		assertOneMassBoundaryPoints("one_mass.gif", controlPoints);
		
		controlPoints = new Vector<Point>();
		controlPoints.add(new Point(4, 4));
		controlPoints.add(new Point(5, 4));
		controlPoints.add(new Point(5, 5));
		controlPoints.add(new Point(6, 6));
		
		assertOneMassBoundaryPoints("one_mass_complicated.gif", controlPoints);

		controlPoints = new Vector<Point>();
		// adding the four corners separately,
		// or else the for-loop will add them twice.
		controlPoints.add(new Point(0, 0));
		controlPoints.add(new Point(0, 79));
		controlPoints.add(new Point(79, 79));
		controlPoints.add(new Point(79, 0));

		for(int i = 1; i < 80 - 1; i++) {
			controlPoints.add(new Point(79, i)); //east
			controlPoints.add(new Point(i, 0)); //north
			controlPoints.add(new Point(0, i)); //west
			controlPoints.add(new Point(i, 79)); //south
		}
		
		assertOneMassBoundaryPoints("BWSquare80x80.jpg", controlPoints);
		
		Vector<Mass> masses = getMasses("four_masses.gif");

		controlPoints = new Vector<Point>();
		controlPoints.add(new Point(9, 1));
		controlPoints.add(new Point(9, 2));
		controlPoints.add(new Point(9, 3));
		controlPoints.add(new Point(8, 2));
		assertMassBoundaryPoints(controlPoints, masses.get(0));
		
		controlPoints = new Vector<Point>();
		controlPoints.add(new Point(4, 4));
		controlPoints.add(new Point(5, 4));
		controlPoints.add(new Point(5, 5));
		controlPoints.add(new Point(6, 6));
		assertMassBoundaryPoints(controlPoints, masses.get(1));
		
		controlPoints = new Vector<Point>();
		controlPoints.add(new Point(0, 8));
		controlPoints.add(new Point(0, 9));
		controlPoints.add(new Point(1, 9));
		assertMassBoundaryPoints(controlPoints, masses.get(2));

		controlPoints = new Vector<Point>();
		controlPoints.add(new Point(5, 8));
		controlPoints.add(new Point(6, 8));
		controlPoints.add(new Point(7, 8));
		assertMassBoundaryPoints(controlPoints, masses.get(3));
		
}

	private void assertOneMassBoundaryPoints(String testImageFilename, Vector<Point> controlPoints) {
		Vector<Mass> masses = getMasses(testImageFilename);
		
		Mass firstMass = masses.firstElement();
		assertMassBoundaryPoints(controlPoints, firstMass);
	}

	private void assertMassBoundaryPoints(Vector<Point> controlPoints, Mass firstMass) {
		Vector<Point> boundaryPoints = firstMass.Boundary_Pixels;
		assertEquals(controlPoints.size(), boundaryPoints.size());
		assertTrue(boundaryPoints.containsAll(controlPoints));
	}

	private Vector<Mass> getMasses(String testImageFilename) {
		ROI testRoi = new ROI(testImageFilename );
		ImageData labeledImage = testRoi.massDetect(testRoi.getOriginalImage());

		testRoi.setLabeledImage(labeledImage);
		testRoi.doRegionGrowing();
		testRoi.doFindLabeledMasses();
		Vector<Mass> masses = testRoi.getMasses();
		return masses;
	}
}
